package com.hpsk.enums;

import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ReflectUtil;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * @author wandoupeas
 * @date 2020/08/19
 */
@JsonDeserialize(using = BaseEnumDeserializer.class)
public interface BaseEnum extends Serializable {

    /**
     * 获取枚举编码
     */
    String getCode();

    static <E extends Enum<E> & BaseEnum> E valueOf(String enumValue, Class<E> clazz) {
        E enumm = Enum.valueOf(clazz, enumValue);
        return enumm;
    }

    static <E extends Enum<E> & BaseEnum> E labelOf(String labelValue, Class<E> clazz) {
        LinkedHashMap<String, E> enumMap = EnumUtil.getEnumMap(clazz);
        for (E e : enumMap.values()) {
            if (ReflectUtil.getFieldValue(e, "label").equals(labelValue)) {
                return e;
            }
        }
        throw new IllegalArgumentException("No matching constant for [" + labelValue + "]");
    }

}
